import 'package:flutter/material.dart';
import 'package:schoollifedemo/business/home/page/home_page.dart';
import 'package:schoollifedemo/business/personal/page/personal_home_page.dart';
import 'package:schoollifedemo/business/school/page/home_school_page.dart';
import 'package:schoollifedemo/commonwidget/circle_empty_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_page.dart';

class MainPage extends BaseStatefulPage {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}

class _MainPageState extends BasePageState<MainPage> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        child: Column(
          children: [
            Expanded(
              child: Padding(
                padding: const EdgeInsets.only(),
                child: _pageTrans(_currentIndex),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                  left: 22, right: 22, bottom: 20, top: 5),
              child: CircleEmptyWidget(
                _homeBottomSheet(_currentIndex, iconHeight: 50, iconWidth: 100),
                radius: 50,
                height: 50,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _pageTrans(int index) {
    switch (index) {
      case 0:
        return const HomePage();
      case 1:
        return SchoolPage();
      case 2:
        return const PersonalPage();
      default:
        return Container();
    }
  }

  Widget _homeBottomSheet(
    int index, {
    double? iconWidth,
    double? iconHeight,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        GestureDetector(
          onTap: () {
            _currentIndex = 0;
            setState(() {});
          },
          child: CircleEmptyWidget(
            _iconWidget(
              'assets/images/common/icon_bottom_home_page.png',
              height: 18,
              width: 18,
            ),
            insideColor:
                index == 0 ? const Color(0xFF97CAFF) : Colors.transparent,
            width: iconWidth,
            height: iconHeight,
            radius: 50,
          ),
        ),
        GestureDetector(
          onTap: () {
            _currentIndex = 1;
            setState(() {});
          },
          child: CircleEmptyWidget(
            _iconWidget('assets/images/common/icon_bottom_school_page.png',
                height: 28, width: 28),
            insideColor:
                index == 1 ? const Color(0xFF97CAFF) : Colors.transparent,
            width: iconWidth,
            height: iconHeight,
            radius: 50,
          ),
        ),
        GestureDetector(
          onTap: () {
            _currentIndex = 2;
            setState(() {});
          },
          child: CircleEmptyWidget(
            _iconWidget('assets/images/common/icon_bottom_personal_page.png',
                height: 18, width: 18),
            insideColor:
                index == 2 ? const Color(0xFF97CAFF) : Colors.transparent,
            width: iconWidth,
            height: iconHeight,
            radius: 50,
          ),
        ),
      ],
    );
  }

  Widget _iconWidget(
    String iconName, {
    double? width,
    double? height,
    double? leftPadding,
    double? rightPadding,
    double? topPadding,
    double? bottomPadding,
  }) {
    return Container(
      padding: EdgeInsets.only(
        left: leftPadding ?? 0,
        right: rightPadding ?? 0,
        top: topPadding ?? 0,
        bottom: bottomPadding ?? 0,
      ),
      child: Center(
        child: Image.asset(
          iconName,
          width: width,
          height: height,
        ),
      ),
    );
  }
}
